package com.iwomy.envpro.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.iwomy.envpro.platform.entity.*;
import com.iwomy.envpro.platform.service.TimingStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * rest定时数据统计 风格
 *
 * @author pig
 * @date 2023-08-31 09:28:08
 */
@Service
@Slf4j
public class TimingStatisticsServiceImpl implements TimingStatisticsService {
	@Resource
	private MongoTemplate mongoTemplate;

	@Override
	public void minutelyAvgData() {
		//5分钟前的数据
		LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-5);
		this.sava(localDateTime,LocalDateTime.now(),AirMinutelyAvgDataEntity.class);


	}

	@Override
	public void hourlyAvgData() {
		//1小前的数据
		LocalDateTime localDateTime = LocalDateTime.now().plusHours(-1);
		this.sava(localDateTime,LocalDateTime.now(), AirHourlyAvgDataEntity.class);


	}

	@Override
	public void monthlyAvgData() {
		//1个月前的数据
		LocalDateTime localDateTime = LocalDateTime.now().plusMonths(-1);
		this.sava(localDateTime,LocalDateTime.now() ,AirMonthlyAvgDataEntity.class);
	}

	@Override
	public void quarterlyAvgData() {
		//3个月前的数据
		LocalDateTime localDateTime = LocalDateTime.now().plusMonths(-3);
		this.sava(localDateTime,LocalDateTime.now(), AirQuarterlyAvgDataEntity.class);
	}

	/**
	 * 	根据时间判断查多久的数据
	 * @param beginTime
	 * @param endTime
	 * @param clazz
	 */
	private void sava(LocalDateTime beginTime,LocalDateTime endTime,Class clazz){
		long endMilli = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
		AggregationOperation matchOperation = Aggregation.match(Criteria.where("recordTime").gte(beginTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()).lte(endMilli));
		GroupOperation groupOperation = Aggregation.group("monitorEquipmentId")
				.first("$$ROOT").as("latestRecord")
				.avg("co").as("co")
				.avg("o3").as("o3")
				.avg("ao3").as("ao3")
				.avg("so2").as("so2")
				.avg("no2").as("no2")
				.avg("pm25").as("pm25")
				.avg("apm25").as("apm25")
				.avg("pm10").as("pm10")
				.avg("apm10").as("apm10");
		AggregationOperation replaceRootOperation = Aggregation.replaceRoot().withValueOf("$latestRecord");
		Aggregation aggregation = Aggregation.newAggregation(matchOperation,  groupOperation,replaceRootOperation);
		AggregationResults<AirRealTimeDataEntity> results = mongoTemplate.aggregate(aggregation, "airRealTimeDataEntity", AirRealTimeDataEntity.class);
		List<AirRealTimeDataEntity> mappedResults = results.getMappedResults();
//		List<AirMinutelyAvgDataEntity> airMinutelyAvgDataEntities = BeanUtil.copyToList(mappedResults, AirMinutelyAvgDataEntity.class);
		mappedResults.stream().forEach(x->{
			x.setId(IdUtil.getSnowflakeNextId());
			x.setRecordTime(endMilli);
		});
		mongoTemplate.insert(mappedResults,clazz);
	}


}